<template>
  <BasicDrawer
    v-bind="$attrs"
    @register="registerDrawer"
    :title="getTitle"
    :width="adaptiveWidth"
    @ok="handleSubmit"
    :showFooter="showFooter"
    destroyOnClose
  >
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts" setup>
import { defineComponent, ref, computed, unref, useAttrs } from "vue";
import { BasicForm, useForm } from "/@/components/Form/index";
import { formSchema } from "./user.data";
import { BasicDrawer, useDrawerInner } from "/@/components/Drawer";
import { add, edit } from "./index.api";
import { useDrawerAdaptiveWidth } from "/@/hooks/jeecg/useAdaptiveWidth";
import { getTenantId } from "/@/utils/auth";
import { useSuccess } from "/@/hooks/rw/handleSuccess";
const { rhandleSuccess } = useSuccess();
// 声明Emits
const emit = defineEmits(["success", "register"]);
const attrs = useAttrs();
const isUpdate = ref(true);
const rowId = ref("");
const departOptions = ref([]);
//表单配置
const [
  registerForm,
  { setProps, resetFields, setFieldsValue, validate, updateSchema },
] = useForm({
  labelWidth: 90,
  schemas: formSchema,
  showActionButtonGroup: false,
});
// TODO [VUEN-527] https://www.teambition.com/task/6239beb894b358003fe93626
const showFooter = ref(true);
//表单赋值
const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
  await resetFields();
  showFooter.value = data?.showFooter ?? true;
  setDrawerProps({ confirmLoading: false, showFooter: showFooter.value });
  isUpdate.value = !!data?.isUpdate;
  if (unref(isUpdate)) {
    rowId.value = data.record.id;
    //租户信息定义成数组
    if (data.record.relTenantIds && !Array.isArray(data.record.relTenantIds)) {
      data.record.relTenantIds = data.record.relTenantIds.split(",");
    } else {
      //【issues/I56C5I】用户管理中连续点两次编辑租户配置就丢失了
      //data.record.relTenantIds = [];
    }

    //查所属部门/赋值
    //负责部门/赋值
    data.record.departIds &&
      !Array.isArray(data.record.departIds) &&
      (data.record.departIds = data.record.departIds.split(","));
    //update-begin---author:zyf   Date:20211210  for：避免空值显示异常------------
    data.record.departIds = data.record.departIds == "" ? [] : data.record.departIds;
    //update-begin---author:zyf   Date:20211210  for：避免空值显示异常------------
  }
  //处理角色用户列表情况(和角色列表有关系)
  data.selectedroles && (await setFieldsValue({ selectedroles: data.selectedroles }));
  //编辑时隐藏密码/角色列表隐藏角色信息/我的部门时隐藏所属部门
  //update-begin---author:wangshuai ---date:20230522  for：【issues/4935】租户用户编辑界面中租户下拉框未过滤，显示当前系统所有的租户------------
  //update-end---author:wangshuai ---date:20230522  for：【issues/4935】租户用户编辑界面中租户下拉框未过滤，显示当前系统所有的租户------------
  // 无论新增还是编辑，都可以设置表单值
  if (typeof data.record === "object") {
    setFieldsValue({
      ...data.record,
    });
  }
  // 隐藏底部时禁用整个表单
  //update-begin-author:taoyan date:2022-5-24 for: VUEN-1117【issue】0523周开源问题
  setProps({ disabled: !showFooter.value });
  //update-end-author:taoyan date:2022-5-24 for: VUEN-1117【issue】0523周开源问题
});
//获取标题
const getTitle = computed(() => (!unref(isUpdate) ? "新增用户" : "编辑用户"));
const { adaptiveWidth } = useDrawerAdaptiveWidth();

//提交事件
async function handleSubmit() {
  try {
    let values = await validate();
    let res;
    setDrawerProps({ confirmLoading: true });
    values.userIdentity === 1 && (values.departIds = "");
    let isUpdateVal = unref(isUpdate);
    //提交表单
    if (isUpdateVal) {
      res = await edit(values.id, values);
    } else {
      res = await add(values);
    }
    rhandleSuccess(res, () => {});
    //关闭弹窗
    closeDrawer();
    //刷新列表
    emit("success", { isUpdateVal, values });
  } finally {
    setDrawerProps({ confirmLoading: false });
  }
}
</script>
